<template>
  <div class='ele-body'>
    <!-- 搜索表单 -->
    <div class='block-interval'>
      <a-card :bordered='false'>
        <a-form layout='inline' :model='where'>
          <a-row>
            <a-form-item label='应用名称:'>
              <a-input v-model:value.trim='where.appName' placeholder='请输入应用名称' allow-clear />
            </a-form-item>
            <a-form-item label='应用编码'>
              <a-input v-model:value.trim='where.appCode' placeholder='请输入应用编码' allow-clear />
            </a-form-item>
            <a-form-item class='ele-text-center'>
              <a-space>
                <a-button type='primary' @click='reload'>查询</a-button>
                <a-button @click='reset'>重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered='false'>
        <ele-pro-table
          ref='table'
          row-key='appId'
          :datasource='url'
          :columns='columns'
          :where='where'
          v-model:selection='selection'
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type='primary' @click='openEdit()'>
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>

          <!-- table列表状态栏 -->
          <!-- 1是激活，2是禁用 -->
          <template #state='{ record }'>
            <a-switch :checked="record.activeFlag === 'Y'" @change='checked => editActive(checked, record)' />
          </template>

          <!-- table操作栏按钮 -->
          <template #action='{ record }'>
            <a-space>
              <a @click='openEdit(record)'>修改</a>
              <a-divider type='vertical' />
              <a-popconfirm title='确定要删除此应用吗？' @confirm='remove(record)'>
                <a class='ele-text-danger'>删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

  <!-- 编辑弹窗 -->
  <sys-app-edit v-model:visible='showEdit' :data='current' @done='reload' />
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import SysAppEdit from './app-edit';
import { SysAppApi } from '@/api/SysAppApi';

export default {
  name: 'SysApp',
  components: {
    PlusOutlined,
    SysAppEdit
  },
  data() {
    return {
      // 表格数据接口
      url: '/sysApp/page',
      // 表格列配置
      columns: [
        {
          key: 'index',
          width: 48,
          customRender: ({ index }) => this.$refs.table.tableIndex + index
        },
        {
          title: '应用名称',
          dataIndex: 'appName',
          sorter: true
        },
        {
          title: '应用编码',
          dataIndex: 'appCode',
          sorter: true
        },
        {
          title: '是否激活',
          dataIndex: 'activeFlag',
          sorter: true,
          align: 'center',
          slots: { customRender: 'state' }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          slots: { customRender: 'action' }
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author chenjinlong
     * @date 2021/4/2 17:03
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author chenjinlong
     * @date 2021/4/2 17:03
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除单个
     *
     * @author chenjinlong
     * @date 2021/4/2 17:03
     */
    async remove(row) {
      const result = await SysAppApi.del({ appId: row.appId });
      this.$message.success(result.message);
      this.reload();
    },

    /**
     * 打开编辑弹窗
     *
     * @author chenjinlong
     * @date 2021/4/2 17:03
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /**
     * 设置默认应用
     *
     * @author chenjinlong
     * @date 2021/4/2 17:04
     */
    async editActive(checked, row) {
      const appId = row.appId;
      // 激活状态：Y-激活，N-未激活
      const activeFlag = checked ? 'Y' : 'N';
      const result = await SysAppApi.active({ appId, activeFlag });
      this.$message.success(result.message);
      this.reload();
    }
  }
};
</script>

<style scoped>
/*搜索框与表格之间的间隙*/
.block-interval {
  margin-bottom: 10px;
}
</style>
